package com.atguigu.userconservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.commonutils.utils.MD5;
import com.atguigu.servicebase.VO.LoginVo;
import com.atguigu.servicebase.VO.RegisterVo;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.userconservice.entity.Author;
import com.atguigu.userconservice.mapper.AuthorMapper;
import com.atguigu.userconservice.service.AuthorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-09
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements AuthorService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 注册方法
     * @param registerVo
     */
    @Override
    public void registerInfo(RegisterVo registerVo) {

        //获得注册信息
        String nickname = registerVo.getNickname();
        String authorMobile = registerVo.getAuthorMobile();
        String password = registerVo.getPassword();

        //验证注册信息
        if(StringUtils.isEmpty(nickname) || StringUtils.isEmpty(authorMobile) || StringUtils.isEmpty(password) ){
            throw new GuliException(2000001,"信息为空");
        }
//
//        //在redis中获取验证码进行校验
//        String moblecode = redisTemplate.opsForValue().get(authorMobile);
//        if(!code.equals(moblecode)){
//            throw new GuliException(2000001,"验证码错误");
//        }

        //校验数据库中是否有相同的手机号
        Integer count = baseMapper.selectCount(new QueryWrapper<Author>().eq("author_mobile", authorMobile));
        if(count.intValue() > 0){
            throw new GuliException(2000001,"已存在手机号");
        }

        //将注册信息放入数据库
        Author author = new Author();
        author.setPassword(registerVo.getPassword());
        author.setAuthorName(registerVo.getNickname());
        author.setAuthorMobile(registerVo.getAuthorMobile());

        //将密码加密
        String oldpassword = author.getPassword();
        String newpassword = MD5.encrypt(oldpassword);
        author.setPassword(newpassword);
        author.setHead("https://bookshore-ords.oss-cn-beijing.aliyuncs.com/1aa82e824cd10f7edcb5c82b8777e643.jpg");
        int insert = baseMapper.insert(author);
        if(insert == 0){
            throw new GuliException(2000001,"注册失败");
        }


    }

    /**
     * 登陆方法
     * @param loginVo
     * @return
     */
    @Override
    public String logininfo(LoginVo loginVo) {
        //获得登录信息
        String authorMobile = loginVo.getAuthorMobile();
        String password = loginVo.getPassword();

        //校验是否为空
        if(StringUtils.isEmpty(authorMobile) || StringUtils.isEmpty(password)){
            throw new GuliException(2000001,"手机或密码错误");
        }

        //验证手机号
        QueryWrapper<Author> mobliewrapper = new QueryWrapper<>();
        mobliewrapper.eq("author_mobile",authorMobile);
        Author authorinfo = baseMapper.selectOne(mobliewrapper);
        if(authorinfo == null){
            throw new GuliException(2000001,"手机或密码错误");
        }

        //验证密码
        String authorinfoPassword = authorinfo.getPassword();
        String encrypt = MD5.encrypt(password);
        if(!authorinfoPassword.equals(encrypt)){
            throw new GuliException(2000001,"手机或密码错误");
        }

        //转换成token字符串
        String jwtToken = JwtUtils.getJwtToken(authorinfo.getId(), authorinfo.getAuthorName());

        return jwtToken;
    }

    /**
     * 根据token获得用户信息
     * @param memberId
     * @return
     */
    @Override
    public Author getAuthorInfo(String memberId) {
        Author author = baseMapper.selectById(memberId);
        return author;
    }

    @Override
    public void addauthoeinfo(Author author) {
        baseMapper.insert(author);
    }


}
